var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var cookie = require('cookie-parser');
var session = require('express-session');
var default_config = require('./config/deault.js');

//配置静态文件目录
app.use(express.static('public'));
//设置视图目录
app.set('views', './views');
//设置模板引擎
app.set('view engine', 'ejs');

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({
	extended: true
})); // for parsing application/x-www-form-urlencoded
app.use(cookie());
app.use(session({
	'secret': 'WBLOG',
	'resave': true,
	'saveUninitialized': true
}));

app.get('/', function(req, res) {
	res.redirect('/blog/index');
});
//单文件上传地址
var upload = require('./utils/uoloadfile.js');
app.post('/upload', upload.upload.single('file'), function(req, res, next) {
	if (typeof req.file === 'undefined' || typeof req.file.path === 'undefined') {
		res.json({
			status: false,
			data: ''
		});
	} else {
		res.json({
			status: true,
			data: req.file.path.replace('public', '')
		});
	}
});

app.use(function(req, res, next) {
	res.locals.user = req.session.user;
	next();
});
//加载路由
var user = require('./routes/user.js')(app);
var blog = require('./routes/blog.js')(app);
var comment = require('./routes/comment.js')(app);
var userApi = require('./api/routes/user.js')(app);
var blogApi = require('./api/routes/blog.js')(app);
app.listen(default_config.port, function() {
	console.log('ok');
});